﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace BizElements.Generator
{
    public partial class DotNetProjectCtl : UserControl, IProjectUserControl
    {
        VariableValueBus variableBus;

        public DotNetProjectCtl()
        {
            InitializeComponent();
            // Generator properties are set/pulled on demand, ie. when generator user control requests it.
            this.uiGenerator.BuildGeneratorProperties = this.CreateGeneratorProperties;
            this.uiGenerator.BuildGenerator = this.CreateGenerator;
            this.variableBus = new VariableValueBus(this.GetVariableValue, GetSuppressedVariables());
            this.uiTemplateList.VariableBus = this.variableBus;
        }

        private void uiAssembly_AssemblySelected(object sender, EventArgs e)
        {
            this.uiMainTab.SelectedTab = this.uiCodeOptionsPage;
            this.uiClassList.Enabled = true;
            this.uiClassList.Assembly = this.uiAssembly.Assembly;
            this.uiGenerator.Enabled = true;
        }

        public event EventHandler WriteProjectSettingsDemanded;

        #region Create generator.

        private DotNetProjectGeneratorProperties CreateGeneratorProperties()
        {
            if (this.WriteProjectSettingsDemanded != null)
                WriteProjectSettingsDemanded(this, new EventArgs());

            return new DotNetProjectGeneratorProperties()
            {
                ItemTemplates = this.uiTemplateList.Templates,
                LoadedAssembly = this.uiAssembly.Assembly,
                OutputFolder = this.uiGenerator.OutputDirectory,
                SelectedItems = this.uiClassList.SelectedTypeFullNames,
                AllComponentVariables = this.uiClassList.GetAllComponentVariables()
            };
        }

        private DotNetProjectGenerator CreateGenerator(ICodeGeneratorProperties properties)
        {
            return new DotNetProjectGenerator((DotNetProjectGeneratorProperties)properties);
        }

        #endregion

        #region Project settings.

        public void ReadProjectSettings(BizElementsSettings projectSettings)
        {
            this.uiMainTab.SelectedTab = this.uiSourcePage;
            this.uiAssembly.ReadSettings(projectSettings);
            this.uiClassList.ReadSettings(projectSettings);
            this.uiTemplateList.ReadProjectSettings("ClassTemplate", projectSettings);
            this.uiGenerator.ReadSettings(projectSettings);
        }

        public void WriteProjectSettings(BizElementsSettings projectSettings)
        {
            this.uiAssembly.WriteSettings(projectSettings);
            if (this.uiClassList.Enabled)
                this.uiClassList.WriteSettings(projectSettings);
            if (this.uiTemplateList.Enabled)
                this.uiTemplateList.WriteProjectSettings("ClassTemplate", projectSettings);
            if (this.uiGenerator.Enabled)
                this.uiGenerator.WriteSettings(projectSettings);
        }

        #endregion

        #region Variables bus.

        private string GetVariableValue(string variableName)
        {
            return null;
        }

        private static string[] GetSuppressedVariables()
        {
            string semicolDelimited = Properties.Settings.Default.DotNetProjectCtl_SuppressedVariables ?? "";
            List<string> varNames = new List<string>();
            foreach (string variable in semicolDelimited.Split(';'))
            {
                if (!string.IsNullOrEmpty(variable))
                    varNames.Add(variable);
            }

            return varNames.ToArray();
        }

        #endregion

        private void uiClassList_TemplatesRequested(object sender, EventArgs e)
        {
            this.uiClassList.SetTemplates(this.uiTemplateList.Templates);
        }
    }
}
